<?php
namespace Admin\Controller;

/**
 * 领券中心
 * Class GiftcodeController
 * @package Admin\Controlle
 */
class GiftcodeController extends BaseController {
    public $local_id;

    public function __construct()
    {
        parent::__construct();
        $this->local_id = session('userInfo')['local_id'];
    }

    /**
     * 领券列表
     */
    public function index(){

        $time = date('Y-m-d H:i:s', time());
//        $type = I('type');
//        if ($type == 'timeout') {
//            $where['last_time'] = ['LT', $time]; // 结束
//        } else {
//            $where['last_time'] = ['EGT', $time];// 未结束
//        }

        $giftcode = M('b2c_giftcode');

        // 排序
        $order = I('param.order', 'desc');
        $order == 'asc' ? 'desc' : 'asc';
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign(['order' => $sunxu]);
        $order = ['id' => $order];

        $where['local_id'] = $this->local_id;

        $count = $giftcode->where($where)->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出

        $giftcode_msg = $giftcode->where($where)->order($order)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        foreach ($giftcode_msg as &$v) {
            $v['type'] = $this->giftcodeType($v['type']);
        }

        $this->assign([
            'giftcode_msg' => $giftcode_msg,
            'show' => $show
        ]);
        $this->display();
    }

    /**
     * 获取类型
     * @param $i
     * @return string
     */
    public function giftcodeType($i)
    {
        switch ($i) {
            case 1:
                $msg = '新人礼券包';
                break;
        }
        return $msg;
    }

    /**
     * 添加领券
     */
    public function add(){

        $time = date('Y-m-d H:i:s', time());
        $b2c_discount_coupon = M('b2c_discount_coupon');
        $where['local_id'] = $this->local_id;
        $where['end_time'] = ['EGT', $time];
        $discount_coupon_msg = $b2c_discount_coupon->where($where)->order("id desc")->select(); // 查询进行中的优惠券
        $this->assign('discount_coupon_msg', $discount_coupon_msg);

        if($_POST) {
            $type = I('type'); // 领取类型
            $name = I('name'); // 领取名称
            $des = I('des'); // 介绍
            $status = I('status'); // 启用状态
            $start_time = I('start_time'); // 开始时间
            $end_time = I('end_time'); // 结束时间
            $img = I('img'); // 图片
            $gifg = I('gifg'); // 赠送礼券

            if (empty($type) || empty($name) || empty($start_time) || empty($end_time) || empty($gifg)) {
                //$this->error('请认真填写信息');
            }
            $gifg = implode(',', $gifg);

            if (!empty($img)) {
                $img = '/Uploads/'.$img;
            }

            if (strtotime($start_time) >= strtotime($end_time)) {
                $this->error('开始时间不能大于结束时间');
            }

            // 添加券
            $data = [
                'type' => $type,
                'local_id' => $this->local_id,
                'name' => $name,
                'msg' => $des,
                'status' => $status,
                'first_time' => $start_time,
                'last_time' => $end_time,
                'gifg' => $gifg,
                'img' => $img,
                'create_time' => $time,
            ];
            $giftcode = M('b2c_giftcode')->add($data);
            if ($giftcode) {
                $this->success('添加成功','/admin/Giftcode/index');
            } else {
                $this->error('添加失败');
            }
        } else {
            $this->display();
        }

    }

    public function update(){
        $id = I('id');
        if (empty($id)) {
            $this->error('报错了^_^');
        }
        $giftcode = M('b2c_giftcode');
        $giftcode_msg = $giftcode->where(['local_id' => $this->local_id, 'id' => $id])->find();

        $time = date('Y-m-d H:i:s', time());
        $b2c_discount_coupon = M('b2c_discount_coupon');
        $where['local_id'] = $this->local_id;
        $where['end_time'] = ['EGT', $time];
        $discount_coupon_msg = $b2c_discount_coupon->where($where)->order("id desc")->select(); // 查询进行中的优惠券
        $this->assign('discount_coupon_msg', $discount_coupon_msg);
        $gifg = explode(',', $giftcode_msg['gifg']);
        $this->assign('gifg', $gifg);

        // 修改数据
        if (IS_POST){
            // 添加券
            $type = I('type'); // 领取类型
            $name = I('name'); // 领取名称
            $des = I('des'); // 介绍
            $status = I('status'); // 启用状态
            $start_time = I('start_time'); // 开始时间
            $end_time = I('end_time'); // 结束时间
            $img = I('img'); // 图片
            $gifg = I('gifg'); // 赠送礼券

            if (empty($type) || empty($name) || empty($start_time) || empty($end_time) || empty($gifg)) {
                //$this->error('请认真填写信息');
            }
            $gifg = implode(',', $gifg);

            if (!empty($img)) {
                $img = '/Uploads/'.$img;
            }

            if (strtotime($start_time) >= strtotime($end_time)) {
                $this->error('开始时间不能大于结束时间');
            }

            // 添加券
            $data = [
                'type' => $type,
                'local_id' => $this->local_id,
                'name' => $name,
                'msg' => $des,
                'status' => $status,
                'first_time' => $start_time,
                'last_time' => $end_time,
                'gifg' => $gifg,
                'create_time' => $time,
            ];
            if (!empty($img)) {
                $data['img'] = $img;
            }

            $giftcode = M('b2c_giftcode')->where(['id' => $id])->save($data);
            if ($giftcode) {
                $this->redirect('/admin/Giftcode/index');
            } else {
                $this->$giftcode('修改失败');
            }
            exit;
        }

        $this->assign(
            ['giftcode_msg' => $giftcode_msg]
        );
        $this->display();
    }

}